using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for Contacts
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class Contacts : SqlServerDAO
    {
        #region Contacts Private Fields

		private Int32 _id;
		private String _senderName;
		private String _senderEmail;
		private String _senderPhone;
		private String _senderMessage;
		private DateTime _sendTime;

        #endregion

        #region Contacts Constractors

		public Contacts(){}

		public Contacts
		(
			Int32 id,
			String senderName,
			String senderEmail,
			String senderPhone,
			String senderMessage,
			DateTime sendTime

		)
		{
			this._id = id;
			this._senderName = senderName;
			this._senderEmail = senderEmail;
			this._senderPhone = senderPhone;
			this._senderMessage = senderMessage;
			this._sendTime = sendTime;
		}

        #endregion

        #region Contacts Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public String SenderName
		{
			get{ return this._senderName; }
			set{ this._senderName = value; }
		}

		public String SenderEmail
		{
			get{ return this._senderEmail; }
			set{ this._senderEmail = value; }
		}

		public String SenderPhone
		{
			get{ return this._senderPhone; }
			set{ this._senderPhone = value; }
		}

		public String SenderMessage
		{
			get{ return this._senderMessage; }
			set{ this._senderMessage = value; }
		}

		public DateTime SendTime
		{
			get{ return this._sendTime; }
			set{ this._sendTime = value; }
		}


        #endregion

        #region Contacts Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewContacts
        (
			String senderName,
			String senderEmail,
			String senderPhone,
			String senderMessage,
			DateTime sendTime

        )
        {
            //creating an empty Contacts object
            Contacts contacts = new Contacts();
            contacts.SenderName = senderName;
			contacts.SenderEmail = senderEmail;
			contacts.SenderPhone = senderPhone;
			contacts.SenderMessage = senderMessage;
			contacts.SendTime = sendTime;

            return Contacts.CreateNewContacts(contacts);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateContacts
        (
			Int32 id,
			String senderName,
			String senderEmail,
			String senderPhone,
			String senderMessage,
			DateTime sendTime

        )
        {
            Contacts contacts = Contacts.GetContactsById(id);
            contacts.SenderName = senderName;
			contacts.SenderEmail = senderEmail;
			contacts.SenderPhone = senderPhone;
			contacts.SenderMessage = senderMessage;
			contacts.SendTime = sendTime;

            return Contacts.UpdateContacts(contacts);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteContacts
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Contacts"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTACTS_DELETECONTACTS);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteContactsList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in Contacts"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTACTS_DELETECONTACTSLIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
        #endregion

        #region Contacts Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewContacts(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateContacts(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Contacts> GetAllContacts()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTACTS_GETALLCONTACTS);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateContactsCollectionFromReader);
            CustomCollection<Contacts> objCollection = ((CustomCollection<Contacts>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Contacts> GetAllContactsPaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTACTS_GETALLCONTACTSPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateContactsCollectionFromReader);
            CustomCollection<Contacts> objCollection = ((CustomCollection<Contacts>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllContactsPagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTACTS_GETALLCONTACTSPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Contacts GetContactsById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Contacts"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTACTS_GETCONTACTSBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateContactsCollectionFromReader);
            CustomCollection<Contacts> objCollection = ((CustomCollection<Contacts>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (Contacts)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewContacts(Contacts newContacts)
        {
            // validate Parameters
            if (newContacts == null)
                throw (new ArgumentNullException("newContacts in Contacts"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@SenderName", SqlDbType.NVarChar, 250, ParameterDirection.Input, newContacts.SenderName);
			AddParameterToSqlCmd(sqlCmd, "@SenderEmail", SqlDbType.NVarChar, 250, ParameterDirection.Input, newContacts.SenderEmail);
			AddParameterToSqlCmd(sqlCmd, "@SenderPhone", SqlDbType.NVarChar, 50, ParameterDirection.Input, newContacts.SenderPhone);
			AddParameterToSqlCmd(sqlCmd, "@SenderMessage", SqlDbType.NVarChar, 500, ParameterDirection.Input, newContacts.SenderMessage);
			AddParameterToSqlCmd(sqlCmd, "@SendTime", SqlDbType.DateTime, 0, ParameterDirection.Input, newContacts.SendTime);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTACTS_CREATENEWCONTACTS);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateContacts(Contacts newContacts)
        {
            // validate Parameters
            if (newContacts == null)
                throw (new ArgumentNullException("newContacts"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newContacts.Id);
            AddParameterToSqlCmd(sqlCmd, "@SenderName", SqlDbType.NVarChar, 250, ParameterDirection.Input, newContacts.SenderName);
			AddParameterToSqlCmd(sqlCmd, "@SenderEmail", SqlDbType.NVarChar, 250, ParameterDirection.Input, newContacts.SenderEmail);
			AddParameterToSqlCmd(sqlCmd, "@SenderPhone", SqlDbType.NVarChar, 50, ParameterDirection.Input, newContacts.SenderPhone);
			AddParameterToSqlCmd(sqlCmd, "@SenderMessage", SqlDbType.NVarChar, 500, ParameterDirection.Input, newContacts.SenderMessage);
			AddParameterToSqlCmd(sqlCmd, "@SendTime", SqlDbType.DateTime, 0, ParameterDirection.Input, newContacts.SendTime);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTACTS_UPDATECONTACTS);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region Contacts Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of Contacts custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// Contacts information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateContactsCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of Contacts collection
            CustomCollection<Contacts> colContacts = new CustomCollection<Contacts>();

            //Iterating thriugh the data reader, to generate Contacts collection.

            //getting the Contacts collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the Contacts constructor parameters from the current instance of data reader fields.
                Contacts newContacts = new Contacts
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["sender_name"] == System.DBNull.Value ? String.Empty : (String)returnData["sender_name"],
					returnData["sender_email"] == System.DBNull.Value ? String.Empty : (String)returnData["sender_email"],
					returnData["sender_phone"] == System.DBNull.Value ? String.Empty : (String)returnData["sender_phone"],
					returnData["sender_message"] == System.DBNull.Value ? String.Empty : (String)returnData["sender_message"],
					returnData["send_time"] == System.DBNull.Value ? DateTime.MinValue : (DateTime)returnData["send_time"]

                );
                //adding the Contacts to the collection
                colContacts.Add(newContacts);
            }

            //returns the collection of Contacts 
            return (colContacts);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_CONTACTS_CREATENEWCONTACTS = "spr_Contacts_CreateNewContacts";
        internal const string SPR_CONTACTS_UPDATECONTACTS = "spr_Contacts_UpdateContacts";
        internal const string SPR_CONTACTS_DELETECONTACTS = "spr_Contacts_DeleteContacts";
        internal const string SPR_CONTACTS_DELETECONTACTSLIST = "spr_Contacts_DeleteContactsList";
        internal const string SPR_CONTACTS_GETCONTACTSBYID = "spr_Contacts_GetContactsById";
        internal const string SPR_CONTACTS_GETALLCONTACTS = "spr_Contacts_GetAllContacts";
        internal const string SPR_CONTACTS_GETALLCONTACTSPAGED = "spr_Contacts_GetAllContactsPaged";
        internal const string SPR_CONTACTS_GETALLCONTACTSPAGEDCOUNT = "spr_Contacts_GetAllContactsPagedCount";

		//Foreign key contants
		
        #endregion
    }
}
